package fi.laurea.mp3player;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.google.gson.Gson;

import fi.laurea.jsonbus.Receiver;
import fi.laurea.jsonbus.ReceiverListener;
import fi.laurea.jsonbus.Sender;

public class MP3PlayerUi extends JFrame implements ReceiverListener{
	
	
	private Sender sender;
	private Receiver receiver;
	private Gson gson;
	private JPanel panel;
	
	public MP3PlayerUi(){
		
		this.receiver = new Receiver("MP3Player", "TestClient", this);
		this.sender = new Sender("MP3Player");
		this.gson = new Gson();
		this.panel = new JPanel();
		this.add(panel);
		InstructionMessage message = new InstructionMessage();
		message.setSendFilelist(true);
		message.setReturnID("TestClient");
		
		this.sender.sendToId(message, "TestPlayer");
		this.setVisible(true);
		
	}
	

	@Override
	public void prosessGloballMessage(String json) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void prosessPrivateMessage(String json) {
		FilelistMessage message = (FilelistMessage)gson.fromJson(json, FilelistMessage.class);
		this.panel.removeAll();
		for(String s : message.getFilelist()){
			final JButton button = new JButton();
			button.setText(s);
			button.addActionListener(new ActionListener() {
               

				@Override
				public void actionPerformed(ActionEvent arg0) {
					InstructionMessage message = new InstructionMessage();
					message.setFilename(button.getText());
					
					sender.sendToId(message, "TestPlayer");
					
				}
    } );
			
			panel.add(button);
			
		}
		
	}
	
	public static void main (String[] args){
	new MP3PlayerUi();	
	}

}
